// pages/intro/intro.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: [ '中国语言文学系','历史学系','哲学系','政治与国际关系学院','法学院','社会发展学院','马克思主义学院','外语学院','国际汉语文化学院','体育与健康学院','教育学部','经济与管理学部','心理与认知科学学院','数学科学学院','物理与电子科学学院','化学与分子工程学院','生命科学学院','生态与环境科学学院','地理科学学院','城市与区域科学学院','通信与电子工程学院','计算机科学与技术学院','软件工程学院','数据科学与工程学院','设计学院','传播学院','美术学院','孟宪承书院','大夏书院','光华书院'],
        cnt: 2,
        display: '',
        changeStatus: '结束招募',
        hiddenmodalput: true,
        Number:'',
        Work: '',
        projectId: 0,
        instituteIndex: 0,
        inChange: 0,
        recruitProject:{
          briefDemand: "",
          captainChuangNum: "",
          captainName: "",
          demand: "",
          endTime: "",
          id: 0,
          institute: "",
          introduction: "",
          memberNum: 0,
          name: "",
          planEndTime: "",
          planStartTime: "",
          recruitNum: 0,
          recruitTime: "",
          startTime: "",
          stateUpdateTime: "",
          status: 0,
          tag1: "",
          tag2: "",
          tag3: "",
          teacherApartment: "",
          teacherName: "",
          teacherRank: "",
          displayId:"",       
        },
        noChange: true,
        text: '修改项目简介',
        members: [],
        sessionID: "",
        chuangNum:"",
        DemandHtml:"",
        IntroHtml:"",
    },
   

    // 用户点击状态变更按钮触发的事件
    ChangeStatus(e){
        if(this.data.changeStatus==='结束招募'){
            wx.showModal({
                title: '结束招募',
                content: '项目状态将由招募中变为进行中\n注意：该操作不可逆',
                success: (res) => {
                  if (res.confirm) {
                    //console.log('用户点击确定')
                    wx.request({
                      url: 'https://www.hschuangsai.com/recruitProject/updateStatus',
                      header: {
                        'content-type': 'application/json',
                        'session': this.data.sessionID
                      },
                      data: {
                        projectId: this.data.projectId,
                        newStatus: 1
                      },
                      method: 'POST',
                      success: (result) => {
                        wx.showToast({
                          title: '状态变更成功',
                        })
                        this.setData({
                          changeStatus: '结束项目',
                          status: -1,
                        })
                        console.log("结束招募",result)
                      },
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })     
        }else if(this.data.changeStatus==='结束项目'){
          wx.showModal({
            title: '结束项目',
            content: '项目状态将由进行中变为已结束\n注意：该操作不可逆',
            success: (res) => {
              if (res.confirm) {
                //console.log('用户点击确定')
                wx.request({
                  url: 'https://www.hschuangsai.com/recruitProject/updateStatus',
                  header: {
                    'content-type': 'application/json',
                    'session': this.data.sessionID
                  },
                  data: {
                    projectId: this.data.projectId,
                    newStatus: -1
                  },
                  method: 'POST',
                  success: (result) => {
                  console.log(result)
                    wx.showToast({
                      title: '状态变更成功',
                    })
                    this.setData({
                      changeStatus: '已结束',
                      status: -1
                    })
                  },
                })
              } else if (res.cancel) {
                //console.log('用户点击取消')
              }
            }
          })
            
        }
    },

    //点击“申请成为展示项目”触发的事件
    display(e){
      wx.showModal({
        title: '请先完善项目信息',
        success: (res) => {
          wx.navigateTo({
            url: '../forDisplay/forDisplay?name='+this.data.recruitProject.name+'&captain='+this.data.recruitProject.captainName+'&institute='+this.data.recruitProject.institute+'&displayId='+this.data.recruitProject.displayId+'&recruitProjectId='+this.data.projectId,
          })
        }
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            display: options.display,
            projectId: options.id
        })

        // 取出sessionID缓存
        this.setData({
            sessionID: wx.getStorageSync('sessionID'),
            chuangNum: wx.getStorageSync('chuangNum')
        })

        if(options.display==1){
          this.setData({
            name: options.name
          })
        }
        //向后端发请求，得到目前收到的简历份数
        if(options.display==1){
          wx.request({
            url: 'https://www.hschuangsai.com/recruitProject/applications',
            header: {
              'content-type': 'application/json',
              'session': this.data.sessionID
            },
            data: {
              projectId: this.data.projectId
            },
            method: "GET",
            success: (result) => {
              this.setData({
                cnt: result.data.data
              })
              console.log("简历数",result)
            },
          })
        }
        // 得到项目详情
        wx.request({
          url: 'https://www.hschuangsai.com/recruitProject/info',
          header: {
            'content-type': 'application/json',
            'session': this.data.sessionID
          },
          data: {
            projectId: this.data.projectId
          },
          method: 'GET',
          success: (result) => {
            console.log(result)
            if(result.data.code != 0){
              wx.showToast({
                title: '获取接口数据失败',
                icon: 'none'
              })
            }else{
            this.setData({
              recruitProject: result.data.data.recruitProject,
              members: result.data.data.members,
              IntroHtml:result.data.data.recruitProject.introduction,
              DemandHtml: result.data.data.recruitProject.demand,
            })
            var WxParse = require('../../wxParse/wxParse.js');
            var data=this.data;
            console.log("project data",result.data.data.recruitProject)
            WxParse.wxParse('Demand', 'html', data.DemandHtml, this, 10);
            WxParse.wxParse('Intro', 'html', data.IntroHtml, this, 10);
            //console.log(this.data.recruitProject)
            //遍历数组，得到对应学院的下标
            for(var i=0;i<31;i++){
              if(this.data.array[i]===result.data.data.recruitProject.institute){
                this.setData({
                  instituteIndex: i
                })
                break;
              }
            }

            if(result.data.data.recruitProject.status==1){
              this.setData({
                changeStatus: '结束项目'
              })
            }else if(result.data.data.recruitProject.status==-1){
              this.setData({
                changeStatus: '已结束'
              })
            }
          }
        }
        })
        
    },
    // 跳转到简历页面
    gotoApplication(e){
        wx.navigateTo({
          url: '/pages/project/application/application?id='+this.data.projectId,
        })
    },
    // 显示或取消“添加项目成员”的输入框
    modalinput: function () {
        this.setData({
          hiddenmodalput: !this.data.hiddenmodalput
        })
    },
    // 得到输入的创赛号
    getNumber(e){
        this.setData({
            Number: e.detail.value
        })
    },
    // 得到输入的分工
    getWork(e){
        this.setData({
            Work: e.detail.value
        })
    },

    // 提交“添加项目成员”
    confirm(e){
      if(this.data.Number==''){
        wx.showToast({
          title: '请输入创赛号',
          icon: 'none'
        })
      }else if(this.data.Work==''){
        wx.showToast({
          title: '请输入成员分工',
          icon: 'none'
        })
      }else if(this.data.Number==this.data.chuangNum){
        wx.showToast({
          title: '您不可以添加自己作为项目成员',
          icon: 'none'
        })
      }
      else{
        wx.request({
          url: 'https://www.hschuangsai.com/member/add',
          header: {
            'content-type': 'application/json',
            'session': this.data.sessionID
          },
          data:  {
            memberChuangNum: this.data.Number,
            recruitProjectId: this.data.projectId,
            type: "MEMBER",
            Work: this.data.Work
        },
          method: "POST",
          success: (result) => {
            console.log(result)
              wx.showToast({
                title: '邀请发送成功',
              }),
              this.setData({
                hiddenmodalput: !this.data.hiddenmodalput,
                Number: '',
                Work: ''
              })
          },
        })
      }
    },

    // 点击“修改项目简介”按钮
    changeInfo(e){
      if(this.data.text=='修改项目简介'){
        //先缓存富文本的数据
        var str1=JSON.stringify(this.data.IntroHtml)
        var str2=JSON.stringify(this.data.DemandHtml)
        wx.setStorageSync('ChangedIntroHtml', str1)
        wx.setStorageSync('ChangedDemandHtml', str2)
        this.setData({
          text: '保存',
          noChange: false,
          inChange: 1
        })
      }else if(this.data.text=='保存'){
        wx.request({
          url: 'https://www.hschuangsai.com/recruitProject/update',
          method: "POST",
          header: {
            'content-type': 'application/json',
            'session': this.data.sessionID
          },
          data: {
            briefDemand: this.data.recruitProject.briefDemand,
            captainName: this.data.recruitProject.captainName,
            demand: this.data.DemandHtml,
            endTime: "1111-11-11",
            institute: this.data.recruitProject.institute,
            introduction: this.data.IntroHtml,
            name: this.data.recruitProject.name,
            planEndTime: this.data.recruitProject.planEndTime,
            planStartTime: this.data.recruitProject.planStartTime,
            projectId: this.data.projectId,
            recruitNum: this.data.recruitProject.recruitNum,
            recruitTime: "1111-11-11",
            startTime: "1111-11-11",
            status: this.data.recruitProject.status,
            tag1: this.data.recruitProject.tag1,
            tag2: this.data.recruitProject.tag2,
            tag3: this.data.recruitProject.tag3,
            teacherApartment: this.data.recruitProject.teacherApartment,
            teacherName: this.data.recruitProject.teacherName,
            teacherRank: this.data.recruitProject.teacherRank,
            type: "CAPTAIN",
          },
          success: (result) => {
            console.log("修改项目成功",result)
          },
        })
        this.setData({
          text: '修改项目简介',
          noChange: true,
          inChange: 0
        })
        wx.removeStorage({
          key: 'ChangedDemandHtml',
          success (res) {
            console.log(res)
          }
        })
        wx.removeStorage({
          key: 'ChangedIntroHtml',
          success (res) {
            console.log(res)
          }
        })
      }
    },

    // 得到输入的项目简介
    capName:function(e){
      this.setData({
        ['recruitProject.captainName'] : e.detail.value
      })
      //console.log(this.data.recruitProject.captainName)
    },
    prgName:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.name']:val
      })
    },
    tag1:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.tag1']:val
      })
    },
    tag2:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.tag2']:val
      })
    },
    tag3:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.tag3']:val
      })
    },
    memNum:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.memberNum']:val
      })
    },
    recNum:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.recruitNum']:val
      })
    },
    intro:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.introduction ']:val
      })
    },
    teacName:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.teacherNum']: val
      })
    },
    teacRank:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.teacherRank']:val
      })
    },
    teacAPT:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.teacherApartment']:val
      })
    },
    briefDMD:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.briefDemand']:val
      })
    },
    detailDMD:function(e){
      let val=e.detail.value;
      this.setData({
        ['recruitProject.demand']: val
      })
    },

    bindPickerChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          instituteIndex:e.detail.value,
          ['recruitProject.institute']: this.data.array[e.detail.value]
        })
      },

      bindDateChangeStart: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          ['recruitProject.planStartTime']: e.detail.value
        })
      },
      bindDateChangeEnd: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          ['recruitProject.planEndTime']:e.detail.value
        })
      },

      //改变富文本
      changeIntro(e){
        wx.navigateTo({
          url: '../../index/index?page=4',
        })
      },

      changeDemand(e){
        wx.navigateTo({
          url: '../../index/index?page=5',
        })
      },

     /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    if( this.data.inChange == 1){
      console.log('on show')
      if(wx.getStorageSync('ChangedIntroHtml')!=''){
        console.log("缓存数据",wx.getStorageSync('ChangedIntroHtml'))
        this.setData({
          IntroHtml: JSON.parse(wx.getStorageSync('ChangedIntroHtml'))
        })
      }
      if(wx.getStorageSync('ChangedDemandHtml')!=''){
        console.log("缓存数据",wx.getStorageSync('ChangedDemandHtml'))
        this.setData({
          DemandHtml: JSON.parse(wx.getStorageSync('ChangedDemandHtml'))
        })
        
      }
      console.log(wx.getStorageSync('ChangedDemandHtml'))
      var WxParse = require('../../wxParse/wxParse.js');
      var data=this.data;
      WxParse.wxParse('Demand', 'html', data.DemandHtml, this, 10);
      WxParse.wxParse('Intro', 'html', data.IntroHtml, this, 10);
    }
    },    
})